package com.chzc.fm.duotin.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.chzc.fm.duotin.bean.mine.SpecialBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by BillSinla on 2016/7/6.
 */
public class AlbumDao {

    private HistoryHelper historyHelper = null;

    public AlbumDao(Context context){
        historyHelper = new HistoryHelper(context);
    }

    /**
     * 插入专辑信息
     * @param albumBean
     */
    public void insertAlbum(SpecialBean.AlbumBean albumBean){
        SQLiteDatabase db = historyHelper.getWritableDatabase();
        db.execSQL("insert into album (album_id, title, image_url, content_num) values(?,?,?,?)",
                new String[]{albumBean.getId(), albumBean.getTitle(),
                        albumBean.getImage_url(), albumBean.getContent_num()});
        db.close();
    }

    /**
     * 删除专辑的信息
     * @param id
     */
    public void deleteAlbum(String id){
        SQLiteDatabase db = historyHelper.getWritableDatabase();
        db.execSQL("delete from album where album_id = ?",
                new String[]{id});
        db.close();
    }

    /**
     * 更新专辑的信息
     * @param albumBean
     */
    public void updateAlbum(SpecialBean.AlbumBean albumBean){
        SQLiteDatabase db = historyHelper.getWritableDatabase();
        db.execSQL("update album set content_num = ? where album_id = ?",
                new String[]{albumBean.getContent_num(), albumBean.getId()});
        db.close();
    }

    /**
     * 获取全部专辑
     * @return
     */
    public List<SpecialBean.AlbumBean> getAlbums(){
        SQLiteDatabase db = historyHelper.getWritableDatabase();
        List<SpecialBean.AlbumBean> albumBeenList = new ArrayList<SpecialBean.AlbumBean>();
        Cursor cursor = db.rawQuery("select * from album", new String[]{});
        while(cursor.moveToNext()){
            SpecialBean.AlbumBean albumBean = new SpecialBean.AlbumBean();

            albumBean.setId(cursor.getString(cursor.getColumnIndex("album_id")));
            albumBean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            albumBean.setImage_url(cursor.getString(cursor.getColumnIndex("image_url")));
            albumBean.setContent_num(cursor.getString(cursor.getColumnIndex("content_num")));
            albumBeenList.add(albumBean);
        }
        cursor.close();
        db.close();
        return albumBeenList;
    }

    /**
     * 获取专辑
     * @param album_id
     * @return
     */
    public List<SpecialBean.AlbumBean> getAlbum(String album_id){

        SQLiteDatabase db = historyHelper.getWritableDatabase();
        List<SpecialBean.AlbumBean> albumBeenList = new ArrayList<SpecialBean.AlbumBean>();
        Cursor cursor = db.rawQuery("select * from album where album_id = ?", new String[]{album_id});
        while(cursor.moveToNext()){
            SpecialBean.AlbumBean albumBean = new SpecialBean.AlbumBean();
            albumBean.setId(cursor.getString(cursor.getColumnIndex("album_id")));
            albumBean.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            albumBean.setImage_url(cursor.getString(cursor.getColumnIndex("image_url")));
            albumBean.setContent_num(cursor.getString(cursor.getColumnIndex("content_num")));
            albumBeenList.add(albumBean);
        }
        cursor.close();
        db.close();
        return albumBeenList;
    }

    /**
     * 判断专辑是否存在
     * @param album_id
     * @return
     */
    public boolean isExists(String album_id){
        SQLiteDatabase db = historyHelper.getWritableDatabase();
        List<SpecialBean.AlbumBean> albumBeanList = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from album where album_id = ?", new String[]{album_id});
        boolean exists = cursor.moveToNext();
        cursor.close();
        db.close();
        return exists;
    }
}
